﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UI.BudgetManage;
using BL.Client.BudgetManageBL;
using Common.ClassLib;
using Common.UIFunction;
using UI.BudgetManage.ProgramData;

namespace UI.BudgetManage.ChildForm.FinanacialBudgetApprove
{
    public partial class FinancialBudgetApprove : Form
    {
        BudgetManageServerice BMS;
        public FinancialBudgetApprove()
        {
            InitializeComponent();
        }

        private void FinancialBudgetApprove_Load(object sender, EventArgs e)
        {
            this.BMS = ((BMMain)this.MdiParent).BMS;
            this.InitDGV();
            this.InitCB();
            this.ShowBudget();
            
        }

        private void InitDGV()
        {
            new DataGridViewBound(this.dgvFBudgetApprove).BoundColumn(BudgetQueryData.HeaderText,
                         BudgetQueryData.ColumnName, BudgetQueryData.DataPropertyName, BudgetQueryData.Width);
            new DataGridViewBound(this.dgvBudgetItem).BoundColumn(BudgetItemQueryData.HeaderText,
                         BudgetItemQueryData.ColumnName, BudgetItemQueryData.DataPropertyName, BudgetItemQueryData.Width);


            
            DataGridViewButtonColumn approvebutton = new DataGridViewButtonColumn();
            approvebutton.Name = "ApproveButton";
            approvebutton.UseColumnTextForButtonValue = true;
            approvebutton.Text = "批准";
            approvebutton.HeaderText = "预算审核";
            dgvFBudgetApprove.Columns.Add(approvebutton);

            DataGridViewButtonColumn rejectbutton = new DataGridViewButtonColumn();
            rejectbutton.Name = "RejectButton";
            rejectbutton.UseColumnTextForButtonValue = true;
            rejectbutton.Text = "否决";
            rejectbutton.HeaderText = "预算否决";
            dgvFBudgetApprove.Columns.Add(rejectbutton);

            DetailDataGridViewBound details =
                new DetailDataGridViewBound(this.dgvFBudgetApprove,
                   this.dgvBudgetItem, this.plBudgetItem, this.llClose);
            details.BudgetItemShowBound("BudgetName");
            details.OnShowDetails += new DetailDataGridViewBound.ShowDetails(this.BMS.GetBudgetItemInfo);


            DataGridViewCheckBoxColumn checkboxc = new DataGridViewCheckBoxColumn();
            checkboxc.Name = "cbSelect";
            checkboxc.HeaderText = "选择";
            checkboxc.Width = 35;
            dgvFBudgetApprove.Columns.Insert(0, checkboxc);
            
        }

        private void InitCB()
        {
            this.cbBANC.Visible = false;
            DataSet ds = this.BMS.IBA.GetBudgetApproveNeedCountInfo();
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                ComboBoxItem newitem = new ComboBoxItem(dr["budget_approve_needcount_text"].ToString(),
                   dr["budget_approve_needcount"].ToString());
                cbBANC.Items.Add(newitem);
            }
        }

        protected void ShowBudget()
        {
            DataSet ds = this.BMS.GetBudgetInfoByAuth();
            dgvFBudgetApprove.DataSource = ds.Tables[0];
            foreach (DataGridViewRow dgvr in this.dgvFBudgetApprove.Rows)
            {
                dgvr.Cells["BudgetApproveNeedCountText"].Style.ForeColor = System.Drawing.Color.Red;
            }
        }

        
        protected void dgvFinancialBA_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.ColumnIndex.Equals(this.dgvFBudgetApprove.Columns["ApproveButton"].Index))
            {
                string budget_approve_needcount_text =
                    this.dgvFBudgetApprove.Rows[e.RowIndex].Cells["BudgetApproveNeedCountText"].FormattedValue.ToString();
                string budget_approve_needcount = this.GetValueByNeedCountText(budget_approve_needcount_text).ToString().Trim(); 
                string budget_id = this.dgvFBudgetApprove.Rows[e.RowIndex].Cells["BudgetId"].Value.ToString().Trim();
                if (!this.BMS.HaveFBAAuthority(budget_id))
                {
                    MessageBox.Show("当前用户没有财务审批权限！");
                    return;
                }
                if (this.BMS.FBudgetApprove(budget_id, true, budget_approve_needcount))
                    MessageBox.Show("操作成功！该预算已被批准！");
                else
                    MessageBox.Show("操作失败！请检查网络连接！");
                this.ShowBudget();
            }
            else if (e.ColumnIndex.Equals(this.dgvFBudgetApprove.Columns["RejectButton"].Index))
            {
                string budget_approve_needcount_text = 
                    this.dgvFBudgetApprove.Rows[e.RowIndex].Cells["BudgetApproveNeedCountText"].FormattedValue.ToString();
                string budget_approve_needcount = this.GetValueByNeedCountText(budget_approve_needcount_text).ToString().Trim(); 
                string budget_id = this.dgvFBudgetApprove.Rows[e.RowIndex].Cells["BudgetId"].Value.ToString().Trim();
                if (!this.BMS.HaveFBAAuthority(budget_id))
                {
                    MessageBox.Show("当前用户没有财务审批权限！");
                    return;
                }
                if (this.BMS.FBudgetApprove(budget_id, false, budget_approve_needcount))
                    MessageBox.Show("操作成功！该预算已被否决！");
                else
                    MessageBox.Show("操作失败！请检查网络连接！");
                this.ShowBudget();
            }
            
        }

        private void btnFApproveChecked_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dgvFBudgetApprove.Rows)
            {
                if (((DataGridViewCheckBoxCell)dgvr.Cells["cbSelect"]).EditedFormattedValue.Equals(true))
                {
                    string budget_approve_needcount_text = dgvr.Cells["BudgetApproveNeedCountText"].FormattedValue.ToString();
                    string budget_approve_needcount = this.GetValueByNeedCountText(budget_approve_needcount_text).ToString().Trim();
                    string budget_id = dgvr.Cells["BudgetId"].Value.ToString().Trim();
                    if (!this.BMS.HaveFBAAuthority(budget_id))
                    {
                        MessageBox.Show("当前用户没有财务审批权限！");
                        return;
                    }
                    bool bl = this.BMS.FBudgetApprove(budget_id, true, budget_approve_needcount);
                    if (!bl)
                    {
                        MessageBox.Show("第 " +dgvr.Index.ToString() +" 行操作失败！请检查网络连接！");
                        this.ShowBudget();
                        return;
                    }
                        
                }
            }
            MessageBox.Show("操作成功！所选预算已被批准！");
            this.ShowBudget();
        }

        private void btnFRejectChecked_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dgvFBudgetApprove.Rows)
            {
                if (((DataGridViewCheckBoxCell)dgvr.Cells["cbSelect"]).EditedFormattedValue.Equals(true))
                {
                    string budget_approve_needcount_text = dgvr.Cells["BudgetApproveNeedCountText"].FormattedValue.ToString();
                    string budget_approve_needcount = this.GetValueByNeedCountText(budget_approve_needcount_text).ToString(); 
                    string budget_id = dgvr.Cells["BudgetId"].Value.ToString().Trim();
                    if (!this.BMS.HaveFBAAuthority(budget_id))
                    {
                        MessageBox.Show("当前用户没有财务审批权限！");
                        return;
                    }
                    bool bl = this.BMS.FBudgetApprove(budget_id, false, budget_approve_needcount);
                    if (!bl)
                    {
                        MessageBox.Show("第 " + dgvr.Index.ToString() + " 行操作失败！请检查网络连接！");
                        this.ShowBudget();
                        return;
                    }
                }
            }
            MessageBox.Show("操作成功！所选预算已被否决！");
            this.ShowBudget();

        }

        private void dgvFBudgetApprove_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvFBudgetApprove.Columns[e.ColumnIndex].Name == "BudgetApproveNeedCountText" &&
                e.RowIndex != -1)
            {
                cbBANC.Visible = true;

                Rectangle rect = this.dgvFBudgetApprove.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
                cbBANC.Width = rect.Width;
                cbBANC.Height = rect.Height;
                cbBANC.Top = rect.Top;
                cbBANC.Left = rect.Left;

                string banc_text = this.dgvFBudgetApprove.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                int banc = this.GetValueByNeedCountText(banc_text);
                cbBANC.SelectedIndex = Convert.ToInt32(banc) - 1;
            }
            else
                cbBANC.Visible = false;
        }
        private int GetValueByNeedCountText(string budget_approve_needcount_text)
        {
            foreach (object obj in this.cbBANC.Items)
            {
                if (((ComboBoxItem)obj).Text == budget_approve_needcount_text)
                    return Convert.ToInt32(((ComboBoxItem)obj).Value);
            }
            return -1;
        }

        private void cbBANC_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dgvFBudgetApprove.CurrentCell.Value = ((ComboBoxItem)cbBANC.SelectedItem).Text;
            this.cbBANC.Visible = false;
        }

        private void dgvFBudgetApprove_Scroll(object sender, ScrollEventArgs e)
        {
            this.cbBANC.Visible = false;
        }
       

        
    }
}